﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TeamBuy.AccessWebUI.Components.DefaultPages
{
    public partial class Main_DealDetail : System.Web.UI.UserControl
    {
        AccessService.TeamBuyDealService currentDeal = new AccessService.TeamBuyDealService();

        public int GetCurrentDealID()
        {
            lvDeal.DataBind();
            if (lvDeal.Items.Count > 0)
            {
                return Convert.ToInt32(((Literal)(lvDeal.Items[0].FindControl("ltlDealID"))).Text);
            }
            else
            {
                return 0;
            }
        }

        public int GetCurrentDealStatus()
        {
            lvDeal.DataBind();
            if (lvDeal.Items.Count > 0)
            {
                return Convert.ToInt32(((Literal)(lvDeal.Items[0].FindControl("ltlDealStatus"))).Text);
            }
            else
            {
                return 0;
            }
        }

        public string GetCurrentDealTitle()
        {
            lvDeal.DataBind();
            if (lvDeal.Items.Count > 0)
            {
                return ((Literal)(lvDeal.Items[0].FindControl("ltlDealTitle"))).Text;
            }
            else
            {
                return "";
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void ShowTodayDeal()
        {
            adsDeal.SelectCommand = "SELECT Top 1 * FROM [S_Deal] WHERE #" + DateTime.Now + "# BETWEEN DateStarted and DateEnded AND IsCurrentDeal = Yes AND IsTodayMainDeal = Yes AND CityID = " + ((Components.Head)(Page.Master.Master.FindControl("Head1"))).CurrentCityID;
        }

        public void ShowDealByID(int dealID)
        {
            adsDeal.SelectCommand = "SELECT Top 1 * FROM [S_Deal] WHERE DealID = " + dealID;
        }

        public string SetStatus(int dealID, int oldStatus, int currentStatus,string returnString)
        {
            currentDeal.SetDealCurrentStatus(dealID, oldStatus, currentStatus);
            return returnString;
        }

        public string FinishDeal(int dealID, int currentStatus, DateTime dateFinished, string returnString)
        {
            currentDeal.SetDealFinishedDate(dealID,currentStatus, dateFinished);
            return returnString;
        }

        public string SetSuccess(int dealID, DateTime dateSuccessed, string returnString)
        {
            currentDeal.SetDealSuccessedDate(dealID, dateSuccessed);
            return returnString;
        }

        public string SetFinishedDateAndStatus(int dealID, int oldStatus, int currentStatus, DateTime dateFinished, string returnString)
        {
            FinishDeal(dealID, oldStatus, dateFinished, "");
            SetStatus(dealID, oldStatus, currentStatus, "");
            lvDeal.Items.Clear();
            return returnString;
        }
    }
}